﻿using System.Data.SqlClient;
using System.Windows;
using System.Windows.Controls;

namespace 餐饮管理系统
{
    public partial class MainWindow : Window
    {
        private static string[] tables = { "BranchTable", "SupplierTable" };
        private string table = tables[0];
        private bool isBranch = true;
        public bool IsBranch
        {
            get
            {
                return isBranch;
            }
        }
        public void loadCenterItems(int selectedIndex = 0)
        {
            CenterListBox.Items.Clear();
            SqlDataReader dataReader = databaseConnect.GetDataReader("select name from " + table);
            if (selectedIndex < 0)
                selectedIndex = 0;
            while (dataReader.Read())
            {
                string branchName = dataReader[0].ToString().Trim();
                ListBoxItem item = new ListBoxItem();
                item.SetResourceReference(ListBoxItem.StyleProperty, "DepartmentBoxItem");
                item.Content = branchName;
                CenterListBox.Items.Add(item);
            }
            dataReader.Close();
            if (CenterListBox.Items.Count > 0)
            {
                (CenterListBox.Items[selectedIndex] as ListBoxItem).IsSelected = true;
                CenterListBox.ScrollIntoView(CenterListBox.Items[selectedIndex]);
            }
        }

        public void loadCenterItems(string selectedBranchName)
        {
            CenterListBox.Items.Clear();
            SqlDataReader dataReader = databaseConnect.GetDataReader("select name from " + table);

            int selectedIndex = 0;
            int i = -1;
            while (dataReader.Read())
            {
                string branchName = dataReader[0].ToString().Trim();

                ListBoxItem item = new ListBoxItem();
                item.SetResourceReference(ListBoxItem.StyleProperty, "DepartmentBoxItem");
                item.Content = branchName;
                CenterListBox.Items.Add(item);
                i++;
                if (branchName.Equals(selectedBranchName))
                {
                    selectedIndex = i;
                }
            }
            dataReader.Close();
            (CenterListBox.Items[selectedIndex] as ListBoxItem).IsSelected = true;
            CenterListBox.ScrollIntoView(CenterListBox.Items[selectedIndex]);
        }

        private void loadItemSelected()
        {
            if (isBranch)
            {
                loadBranchSelected();
            }
            else
            {
                loadSupplierSelected();
            }
        }

        public void setTable(bool isranch)
        {
            table = isranch ? tables[0] : tables[1];
            isBranch = isranch ? true : false;
            loadCenterItems();
        }

        private int getItemNumber()
        {
            string sql = "select count(name) from " + table;
            object buffer = databaseConnect.ExecuteScaler(sql);
            if (buffer == null)
            {
                return -1;
            }
            else
            {
                return (int)buffer;
            }
        }

        private bool isItemNameExisted(string name)
        {
            string sql = "select count(name) from " + table + " where name = '" + name + "'";
            object buffer = databaseConnect.ExecuteScaler(sql);
            if (buffer == null)
            {
                return false;
            }
            else
            {
                return (int)databaseConnect.ExecuteScaler(sql) > 0;
            }
        }

        private void Center_addButton_Click(object sender, RoutedEventArgs e)
        {
            string branchName = BranchNameTextBox.Text.ToString().Trim();
            if (isItemNameExisted(branchName) == false)
            {
                string sql = "insert into " + table + "(name) "
                            + "values('"
                            + branchName + "')";
                databaseConnect.ExecuteQuery(sql);
                loadCenterItems(branchName);
            }
        }

        private void CenterListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadItemSelected();
            if (isBranch)
            {
                loadStaffs();
                initFinance();
                initOrder();
            }
            else
            {
                loadOrders();
            }
        }
    }



    public partial class App : Application
    {

        private void DeleteBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            string itemName = (sender as Button).Content as string;

            string sql;

            MainWindow mainWindow = (this.MainWindow as MainWindow);

            if (mainWindow.IsBranch)
            {
                sql = "delete from BranchTable where name ='" + itemName + "' "
                     + "delete from StaffTable where branch_name = '" + itemName + "'";
            }
            else
            {
                sql = "delete from SupplierTable where name ='" + itemName + "' "
                    + "delete from SupplierOrderTable where supplier_name = '" + itemName + "'";
            }
            mainWindow.DatabaseConnect.ExecuteQuery(sql);

            ListBox centerListBox = mainWindow.CenterListBox;
            ItemCollection items = centerListBox.Items;
            bool isSelected = false;
            string selectedBranchName = (centerListBox.SelectedItem as ListBoxItem).Content.ToString();

            if (selectedBranchName.Equals(itemName))
            {
                isSelected = true;
            }

            if (isSelected)
            {
                mainWindow.loadCenterItems();
            }
            else
            {
                mainWindow.loadCenterItems(selectedBranchName);
            }
        }
    }
}
